// 文件名：week1\problem\lc594_欧琳琳.cpp
// 提交链接：https://leetcode.cn/problems/longest-harmonious-subsequence/submissions/559411280?envType=problem-list-v2&envId=sliding-window&status=SOLVED
// 594. 哈希表，数组，滑动窗口，计数，排序
// 完成日期：2024/08/28
// c++,使用排序以及双指针遍历

class Solution {
public:
    int findLHS(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int n=nums.size();
        int start=0;
        int maxlength=0;
        for(int end=0;end<n;end++)
        {
            while(nums[end]-nums[start]>1)
            {
                start++;
            }
            if(nums[end]-nums[start]==1)
            {
                maxlength=max(maxlength,end-start+1);
            }
        }
        return maxlength;
    }
};